package cn.xuchunh.campusbbs.base;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.bean.DataList;
import cn.xuchunh.campusbbs.viewholder.FooterViewHolder;

/**
 * Created by XuChunH on 2015/9/12.
 */
public abstract class BaseRecyclerViewAdapter<VH extends ViewHolder, T>
        extends RecyclerView.Adapter<ViewHolder> implements View.OnClickListener {

    private static final int TYPE_FOOTER = -1;

    protected DataList<T> dataList;
    protected Context context;
    protected LayoutInflater inflater;
    private boolean hasMore = true;
    private boolean needFooter = false;
    private OnItemClickListener listener;

    public BaseRecyclerViewAdapter(Context context) {
        this.context = context;
        dataList = new DataList<>();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return onCreateFooterViewHolder(parent);
        } else {
            return onCreateContentViewHolder(parent, viewType);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_FOOTER) {
            onBindFooterViewHolder(holder);
        } else {
            onBindContentViewHolder((VH) holder, position);
            holder.itemView.setTag(R.id.tag_position, position);
            holder.itemView.setOnClickListener(this);
//            holder.itemView.setOnTouchListener(onTouchListener);
            ViewCompat.setTranslationZ(holder.itemView, 0);
        }
    }

    @Override
    public int getItemCount() {
        if (needFooter) {
            return dataList.size() == 0 ? 0 : dataList.size() + 1;
        }
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (needFooter) {
            return position == getItemCount() - 1 ? TYPE_FOOTER : getContentItemViewType(position);
        } else {
            return getContentItemViewType(position);
        }
    }

    public int getContentItemViewType(int position) {
        return 0;
    }

    public Context getContext() {
        return context;
    }

    public void add(T data) {
        dataList.addData(data);
        notifyDataSetChanged();
    }

    public void addArray(T[] dataArray) {
        dataList.addDataArray(dataArray);
        notifyDataSetChanged();
    }

    public void addList(List<T> list) {
        dataList.addDataList(list);
        notifyDataSetChanged();
    }

    public void refresh(List<T> list) {
        dataList.clear();
        dataList.addDataList(list);
        notifyDataSetChanged();
    }

    public T getItem(int position) {
        return dataList.get(position);
    }

    public DataList<T> getDataList() {
        return dataList;
    }

    public boolean isHasMore() {
        return hasMore;
    }

    public void setHasMore(boolean hasMore) {
        this.hasMore = hasMore;
    }

    public boolean isNeedFooter() {
        return needFooter;
    }

    public void setNeedFooter(boolean needFooter) {
        this.needFooter = needFooter;
    }

    public abstract VH onCreateContentViewHolder(ViewGroup parent, int viewType);

    protected ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        return new FooterViewHolder(inflater.inflate(R.layout.item_footer, parent, false));
    }

    protected abstract void onBindContentViewHolder(VH holder, int position);

    protected void onBindFooterViewHolder(ViewHolder holder) {
        FooterViewHolder temp = (FooterViewHolder) holder;
        if (isHasMore()) {
            temp.progress.setVisibility(View.VISIBLE);
            temp.message.setText(R.string.progress_load_more);
        } else {
            temp.progress.setVisibility(View.GONE);
            temp.message.setText(R.string.succeed_no_more);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View item);
    }

    @Override
    public void onClick(final View v) {
        if (v.getTag(R.id.tag_position) != null) {
            if (listener != null) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        listener.onItemClick((int) v.getTag(R.id.tag_position), v);
                    }
                }, 200);
            }
        }
    }

    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                ViewCompat.setTranslationZ(v, context.getResources().getDimensionPixelSize(R.dimen.elevation));
            } else if (action == MotionEvent.ACTION_UP) {
                ViewCompat.setTranslationZ(v, 0);
            }
            return false;
        }
    };

}
